var express = require('express');
var path = require('path');
const { callbackify } = require('util');
var router = express.Router();
var sqlite3 = require('sqlite3').verbose();
var session = require('express-session')

/* GET users listing. */
// 访问login路由时需要先在req的session中保持用户的信息
router.get('/', function(req, res, next) {  
  var db=new sqlite3.Database(path.join(__dirname, "../db/ChangR.s3db"));  //_dirname="D:\checkin\routes
  var oemc={
    cpassword:req.query.cpassword,
    npassword:req.query.npassword,
    confirm:req.query.confirm
  };
  req.session.oemc=oemc;
  console.log('/OEMconfirm_pwd',oemc.cpassword);
  if(req.query.oemc){
    //cpassword需要和用户名匹配
    if(cpassword==req.session.user.password){
      req.session.user.password=npassword;
      res.send("1");
    }else if(cpassword!=req.session.user.password){
      res.send("2");
    }
  }




  /*
  if(req.session.oemc){
    // 可以对用户名/密码格式进行检查, 如果非法, 返回提示信息
try{
    db.serialize(function() {
      //sqlStr = "CREATE TABLE IF NOT EXISTS TBL_meta (ID varchar2(255), label varchar2(255))"
      sqlStr = "SELECT * FROM `GD_user` ";
      db.all(sqlStr, function(err, rows) {   //查询所有数据
          if(req.query.username == 'rows[1]' )console.log('the result is:',rows[0]);
         var q=0;
         var i=0;
         console.log("rows",rows);
          while(rows[i]){
            if(user.username==rows[i].username&&user.password==rows[i].password){
              q=1;
              console.log("biubiu1");
              break;
            }
            else if(user.username==rows[i].username||user.password==rows[i].password){
              q=2;
              console.log("biubiu2");
              break;
            }
            i=i+1;
          }
          if(q==1){
            return res.send('1');
          }
          else if(q==2){
            return res.send('2');
          }
          else if(q==0){
            return res.send('3');
          }
          delete req.session.user;
      });
      db.close();
    });
  }catch(ex){
    console.log(ex);
  }
  }else{
    res.send('4');
  }
  */
});
module.exports = router;
